﻿using ProtoBuf.Message;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

/// <summary>
/// From：张帅
/// PS：自走棋结算Item
/// </summary> 
namespace UI
{
    public class AutoChessBillingInfoItem : MonoBehaviour
    {
        public Text rankTxt, playerName, grade, battleTime, waveCount;
        public GroupSpriteKit rankSprite;
        public TextureKit playerIcon;
        public AutoChess.HeroListItem iconPref;
        public GameObject unFinish, finished;
        public Transform rewardParent, iconParent;

        void SetReward(List<ItemBase> list)
        {
            for(int i = 0; i < list.Count; i++)
            {
                var icon = CommonMethod.GetCommonIcon(rewardParent);
                icon.SetData(list[i].id, list[i].type, list[i].num, true);
                icon.transform.localScale = Vector3.one * 0.6f;
            }
        }

        void SetRank(int rank)
        {
            bool isTop3 = rank > 0 && rank < 4;
            rankSprite.gameObject.SetActive(isTop3);
            rankTxt.gameObject.SetActive(!isTop3);

            if (isTop3)
            {                
                rankSprite.SetSpriteName("UI_paihang_icon_0" + rank);
                rankSprite.GetComponent<Image>().SetNativeSize();
            }
            else
            {               
                rankTxt.text = string.Format(TableTextBlo.GetText("Ranking"),rank.ToString());
            }
        }

        void SetHero(List<AutoChess.HeroData> list)
        {
            iconPref.gameObject.SetActiveEx(true);
            for (int i = 0; i < list.Count; i++)
            {
                var item = Instantiate(iconPref);
                item.InitHero(list[i]);
                TransformUtility.SetParent(item.transform, iconParent, false);
                item.transform.localScale = Vector3.one * 0.6f;
            }
            iconPref.gameObject.SetActiveEx(false);
        }

        public void SetItem(AutoChessPlayerReportInfo data)
        {
            bool isEnd = data.rank != 0;
            unFinish.SetActiveEx(!isEnd);
            finished.SetActiveEx(isEnd);

            var cacheData = AutoChess.AutoChessManager.Ins.cacheData;
            var player = cacheData.GetPlayerData(data.playerId);
            int shipId = player.ShipId == 0 ? Table.DataModel.TableConfig.autochessDefaultShip : player.ShipId;
            var dict = Table.Blo.TableShipBlo.GetTableShip(shipId);
            if (dict != null)
            {
                playerIcon.textureType = TextureKit.TextureType.SHIP;
                playerIcon.TextureName = dict.shipIcon;
            }
            grade.text = Table.Blo.AutoChessBlo.GetGradeById(player.Grade).gradeName;
            playerName.text = player.NickName;

            if (isEnd)
            {
                waveCount.text = data.waveNum.ToString();
                var dateTime = Yunchang.ServerTimerTool.Java2CSTimeLocale(data.endTime);

                var sb = new System.Text.StringBuilder();
                sb.Append(dateTime.Hour.ToString("D2"));
                sb.Append(":");
                sb.Append(dateTime.Minute.ToString("D2"));
                sb.Append(":");
                sb.Append(dateTime.Second.ToString("D2"));

                battleTime.text = sb.ToString();

                SetReward(data.rewards);
                SetRank(data.rank);
                SetHero(player.GetHeroList(AutoChess.GridType.FightGrid));
            }
        }
    }
}
